package comu.hnevc.zh.testdemo.fragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import comu.hnevc.zh.testdemo.MainActivity;
import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.bean.peccancyxiangqing;

public class QueryPeccancyFragment extends Fragment {
     private MainActivity activity;
     private Button insert_btn;
     private List<peccancyxiangqing>  peccancyxiangqingList;
     private ListView carinfo_lv;
     private ListView peccancy_lv;
     private Mybase mybase;
    private Mybase1 mybase1;
     private String carnumber;
     private TextView hade_name;
    public QueryPeccancyFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_queryjieguo,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity= (MainActivity) getActivity();
        insert_btn=activity.findViewById(R.id.id_btn_insert);
         carinfo_lv=activity.findViewById(R.id.id_lv_carinfo);
         peccancy_lv=activity.findViewById(R.id.id_lv_peccancyxiangqing);
         hade_name.setText("查询结果");
         mybase=new Mybase();
         mybase1=new Mybase1();
         carinfo_lv.setAdapter(mybase);
         insert_btn.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 getFragmentManager().beginTransaction()
                         .replace(R.id.id_content_view,new CarPeccancyFragment())
                         .commit();
             }
         });

    }
    class Mybase extends BaseAdapter{

        @Override
        public int getCount() {
            return activity.carInfoList.size();
        }

        @Override
        public Object getItem(int position) {
            return activity.carInfoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=View.inflate(activity,R.layout.listview_carinfo,null);
            final TextView carnumber_tv=view.findViewById(R.id.tv_carnumber);
            carnumber_tv.setText(activity.carInfoList.get(position).getCarnumber());
            TextView pcount=view.findViewById(R.id.id_tv_pcount);
            pcount.setText("未处理查询"+activity.carInfoList.get(position).getPcount()+"次");
            TextView pmoney=view.findViewById(R.id.id_tv_pmoeny);
            pmoney.setText("罚款"+activity.carInfoList.get(position).getPmoneys()+"元");
            TextView pscodes=view.findViewById(R.id.id_tv_pscodes);
            pscodes.setText("扣"+activity.carInfoList.get(position).getPscodes()+"分");
            Button del_btn=view.findViewById(R.id.id_btn_del);
            LinearLayout carinfo_ll=view.findViewById(R.id.id_ll_carinfo);
            carinfo_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    carnumber=carnumber_tv.getText().toString();
                    initData();
                    peccancy_lv.setAdapter(mybase1);

                }
            });

            return view;
        }
    }

    private void initData() {
        peccancyxiangqingList=new ArrayList<>();
        for (int i=0;i<activity.allCarPeccancieList.size();i++){
            if (activity.allCarPeccancieList.get(i).getCarnumber().equals(carnumber)){
                peccancyxiangqing p=new peccancyxiangqing();
                p.setPscode(activity.allCarPeccancieList.get(i).getPscode());
                p.setDatetime(activity.allCarPeccancieList.get(i).getDatetime());
                p.setPaddr(activity.allCarPeccancieList.get(i).getPaddr());
                p.setPremarks(activity.allCarPeccancieList.get(i).getPremarks());
                p.setPmoney(activity.allCarPeccancieList.get(i).getPmoney());
                peccancyxiangqingList.add(p);


            }

        }
    }

class Mybase1 extends BaseAdapter{

    @Override
    public int getCount() {
        return  peccancyxiangqingList.size();
    }

    @Override
    public Object getItem(int position) {
        return peccancyxiangqingList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view1=View.inflate(activity,R.layout.listviwe_peccancyxiangqing,null);
        TextView datetime=view1.findViewById(R.id.id_tv_datetime);
        datetime.setText(peccancyxiangqingList.get(position).getDatetime());
        TextView paddr=view1.findViewById(R.id.id_tv_paddr);
        paddr.setText(activity.allCarPeccancieList.get(position).getPaddr());
        TextView premarks=view1.findViewById(R.id.id_tv_premarks);
        premarks.setText(activity.allCarPeccancieList.get(position).getPremarks());
        TextView pmoney=view1.findViewById(R.id.id_tv_pmoeny);
        pmoney.setText("罚款"+activity.allCarPeccancieList.get(position).getPmoney()+"元");
        TextView pscode=view1.findViewById(R.id.id_tv_pscode);
        pscode.setText("扣分"+activity.allCarPeccancieList.get(position).getPscode()+"分");
        LinearLayout img_ll=view1.findViewById(R.id.id_ll_img);
        img_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.id_content_view,new CarPeccancyFragment())
                        .commit();

            }
        });
        return view1;
    }
}

}